<!-- 采购报表 采购入库统计 -->
<template>
<<<<<<< HEAD
  <view class="app-container">
    <!-- 搜索表单 -->
    <view class="search-form">
      <van-cell-group>
        <van-field
          v-model="queryParams.materialName"
          label="物料名称"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter="handleQuery"
        />
        <van-field
          v-model="queryParams.contractNo"
          label="合同编号"
          placeholder="请输入合同编号"
          clearable
          @keyup.enter="handleQuery"
        />
        <van-field
          readonly
          clickable
          label="签约日期"
          :value="daterangeSignDate.join(' 至 ')"
          placeholder="请选择日期范围"
          @click="showDatePicker = true"
        />
        <van-popup v-model:show="showDatePicker" position="bottom">
          <van-date-picker
            type="date"
            title="选择日期范围"
            :min-date="minDate"
            :max-date="maxDate"
            @confirm="onDateConfirm"
            @cancel="showDatePicker = false"
          />
        </van-popup>
        <van-field
          readonly
          clickable
          label="供应商"
          :value="selectedSupplierLabel"
          placeholder="请选择供应商"
          @click="showSupplierPicker = true"
        />
        <van-popup v-model:show="showSupplierPicker" position="bottom">
          <van-picker
            show-toolbar
            :columns="supplierIdOptions"
            value-key="dictLabel"
            @confirm="onSupplierConfirm"
            @cancel="showSupplierPicker = false"
          />
        </van-popup>
      </van-cell-group>
      <view class="button-group">
        <van-button type="primary" @click="handleQuery">搜索</van-button>
        <van-button plain @click="resetQuery">重置</van-button>
      </view>
    </view>

    <!-- 数据表格 -->
    <view class="table-container">
      <van-list
        v-model:loading="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="loadMore"
      >
        <van-cell v-for="(item, index) in materialList" :key="index" :title="item.materialName">
          <view class="table-row">
            <text>合同编号: {{ item.contractNo }}</text>
            <text>供应商: {{ item.supplierName }}</text>
            <text>型号: {{ item.materialModel }}</text>
            <text>规格: {{ item.materialSpecification }}</text>
            <text>单价: {{ item.price }} 元</text>
            <text>数量: {{ item.quantity }}</text>
            <text>金额: {{ item.amount }} 元</text>
            <text>签约日期: {{ formatDate(item.signDate) }}</text>
          </view>
        </van-cell>
      </van-list>
    </view>

    <!-- 导出按钮 -->
    <view class="export-button">
      <van-button type="warning" plain @click="handleExport">导出</van-button>
    </view>
  </view>
</template>

<script>
import { ref, reactive, onMounted } from "vue";
import dayjs from "dayjs";

// 模拟 API 数据
const mockApi = {
  listMaterial: (params) => {
    const data = Array.from({ length: 10 }, (_, i) => ({
      contractNo: `HT-${i + 1}`,
      supplierName: `供应商-${i + 1}`,
      materialName: `物料-${i + 1}`,
      materialModel: `型号-${i + 1}`,
      materialSpecification: `规格-${i + 1}`,
      materialUnit: "个",
      price: Math.floor(Math.random() * 1000),
      quantity: Math.floor(Math.random() * 100),
      amount: Math.floor(Math.random() * 10000),
      signDate: new Date(),
    }));
    return new Promise((resolve) => {
      setTimeout(() => resolve({ rows: data, total: data.length }), 500);
    });
  },
  exportMaterial: () => {
    return new Promise((resolve) => {
      setTimeout(() => resolve({ msg: "导出成功" }), 500);
    });
  },
};

export default {
  setup() {
    // 查询参数
    const queryParams = reactive({
      pageNum: 1,
      pageSize: 10,
      materialName: "",
      contractNo: "",
      supplierId: null,
      signDate: [],
    });

    // 数据列表
    const materialList = ref([]);
    const loading = ref(false);
    const finished = ref(false);

    // 签约日期选择器
    const daterangeSignDate = ref([]);
    const showDatePicker = ref(false);
    const minDate = new Date(2020, 0, 1);
    const maxDate = new Date();

    // 供应商选择器
    const supplierIdOptions = ref([
      { dictValue: 1, dictLabel: "供应商A" },
      { dictValue: 2, dictLabel: "供应商B" },
      { dictValue: 3, dictLabel: "供应商C" },
    ]);
    const selectedSupplierLabel = ref("");
    const showSupplierPicker = ref(false);

    // 获取数据
    const getList = async () => {
      loading.value = true;
      const params = { ...queryParams };
      if (daterangeSignDate.value.length === 2) {
        params.beginSignDate = daterangeSignDate.value[0];
        params.endSignDate = daterangeSignDate.value[1];
      }
      const response = await mockApi.listMaterial(params);
      materialList.value = [...materialList.value, ...response.rows];
      finished.value = materialList.value.length >= response.total;
      loading.value = false;
    };

    // 加载更多
    const loadMore = () => {
      if (!finished.value) {
        queryParams.pageNum++;
        getList();
      }
    };

    // 搜索
    const handleQuery = () => {
      queryParams.pageNum = 1;
      materialList.value = [];
      finished.value = false;
      getList();
    };

    // 重置
    const resetQuery = () => {
      queryParams.materialName = "";
      queryParams.contractNo = "";
      queryParams.supplierId = null;
      daterangeSignDate.value = [];
      selectedSupplierLabel.value = "";
      handleQuery();
    };

    // 导出
    const handleExport = async () => {
      try {
        const response = await mockApi.exportMaterial(queryParams);
        uni.showToast({
          title: response.msg,
          icon: "success",
        });
      } catch (error) {
        uni.showToast({
          title: "导出失败",
          icon: "none",
        });
      }
    };

    // 日期选择确认
    const onDateConfirm = (event) => {
      daterangeSignDate.value = event.map((item) => dayjs(item).format("YYYY-MM-DD"));
      showDatePicker.value = false;
    };

    // 供应商选择确认
    const onSupplierConfirm = (event) => {
      queryParams.supplierId = event.dictValue;
      selectedSupplierLabel.value = event.dictLabel;
      showSupplierPicker.value = false;
    };

    // 格式化日期
    const formatDate = (date) => dayjs(date).format("YYYY-MM-DD");

    // 初始化
    onMounted(() => {
      getList();
    });

    return {
      queryParams,
      materialList,
      loading,
      finished,
      daterangeSignDate,
      showDatePicker,
      minDate,
      maxDate,
      supplierIdOptions,
      selectedSupplierLabel,
      showSupplierPicker,
      getList,
      loadMore,
      handleQuery,
      resetQuery,
      handleExport,
      onDateConfirm,
      onSupplierConfirm,
      formatDate,
    };
  },
};
</script>

<style lang="scss">
.app-container {
  background-color: #f5f5f5;
  padding: 20px;

  .search-form {
    margin-bottom: 20px;
  }

  .button-group {
    display: flex;
    justify-content: space-between;
    margin-top: 10px;

    .van-button--primary {
      background-color: #4caf50; // 双碳主题绿色
      border-color: #4caf50;
    }

    .van-button--plain {
      color: #4caf50;
      border-color: #4caf50;
    }
  }

  .table-container {
    background-color: #fff;
    border-radius: 8px;
    padding: 10px;

    .table-row {
      display: flex;
      flex-direction: column;
      gap: 5px;
      margin-bottom: 10px;

      text {
        font-size: 14px;
        color: #333;
      }
    }
  }

  .export-button {
    margin-top: 20px;
    text-align: center;

    .van-button--warning {
      background-color: #ff9800; // 警告色
      border-color: #ff9800;
    }

    .van-button--plain {
      color: #ff9800;
      border-color: #ff9800;
    }
  }
}
</style>
=======
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="合同编号" prop="contractNo">
        <el-input
          v-model="queryParams.contractNo"
          placeholder="请输入合同编号"
          clearable

          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商" prop="supplierId">
        <el-select v-model="queryParams.supplierId" placeholder="请选择供应商" clearable filterable >
          <el-option
            v-for="dict in supplierIdOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="物料名称" prop="materialName">
        <el-input
          v-model="queryParams.materialName"
          placeholder="请输入物料名称"
          clearable

          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button  type="primary"   @click="handleQuery">搜索</el-button>
        <el-button    @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain


          @click="handleExport"
        >导出</el-button>
      </el-col>

    </el-row>

    <el-table v-loading="loading" :data="purchaseArriveDetailReportList">
      <el-table-column label="合同编号" align="center" prop="contractNo"  width="200" />
      <el-table-column label="供应商名称" align="center" prop="supplierName"  width="200" />
      <el-table-column label="物料名称" align="center" prop="materialName"  width="200" />
      <el-table-column label="型号" align="center" prop="materialModel"  width="200" />
      <el-table-column label="规格" align="center" prop="materialSpecification"  width="200" />
      <el-table-column label="单位" align="center" prop="materialUnit"  width="200" />
      <el-table-column label="生产批号" align="center" prop="batchNo"  width="200" />
      <el-table-column label="单价" align="center" prop="price"  width="200" />
      <el-table-column label="到货数量" align="center" prop="arriveQuantity"  width="200" />
      <el-table-column label="退货数量" align="center" prop="returnQuantity"  width="200" />
      <el-table-column label="入库数量" align="center" prop="inWarehouseQuantity"  width="200" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listPurchaseArriveDetailReport, exportPurchaseArriveDetailReport } from "@/api/purchaseReport/purchaseArriveDetailReport";

export default {
  name: "PurchaseArriveDetailReport",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购到货详细报表表格数据
      purchaseArriveDetailReportList: [],
      // 供应商id字典
      supplierIdOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contractNo: null,
        supplierId: null,
        supplierName: null,
        materialId: null,
        materialName: null,
        materialCode: null,
        materialModel: null,
        materialSpecification: null,
        materialUnit: null,
        batchNo: null,
        price: null,
        arriveQuantity: null,
        returnQuantity: null,
        inWarehouseQuantity: null
      },
    };
  },
  created() {
    this.getList();
    this.getQueryData("query_supplier").then(res => {
      this.supplierIdOptions = res.data;
    });
  },
  methods: {
    /** 查询采购到货详细报表列表 */
    getList() {
      this.loading = true;
      listPurchaseArriveDetailReport(this.queryParams).then(response => {
        this.purchaseArriveDetailReportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 供应商id字典翻译
    supplierIdFormat(row, column) {
      return this.selectDictLabel(this.supplierIdOptions, row.supplierId);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有采购到货详细报表数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportPurchaseArriveDetailReport(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
>>>>>>> 5b8858b09e35d379956227f2720ff4c2cd0dbd08
